import { NestFactory } from '@nestjs/core'
import { ConfigService } from '@nestjs/config'
import { type NestExpressApplication } from '@nestjs/platform-express'
import { HttpExceptionFilter } from './filters/http-exception'
import { TransformInterceptor } from './interceptor/transform.interceptor'
import { AppModule } from './app.module'
import { ValidationPipe } from './pipe'
import * as path from 'path'

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: ['error', 'warn', 'log']
  })

  const configService: ConfigService<ENV_CONFIG> = app.get(ConfigService)

  app.setGlobalPrefix('api')
  app.enableCors({
    origin: '*',
    exposedHeaders: ['Content-Disposition'],
    methods: ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS', 'HEAD'],
    allowedHeaders: [
      'Content-Type',
      'Content-Length',
      'Authorization',
      'Accept-Language',
      'Content-Language',
      'Accept',
      'X-Requested-With',
      'Origin',
      'Content-Disposition'
    ]
  })

  app.useStaticAssets(path.resolve('public'), {
    maxAge: '1d',
    extensions: ['jpg', 'jpeg', 'png', 'gif'],
    prefix: configService.get('PUBLIC.STATIC', { infer: true })
  })

  app.useGlobalPipes(new ValidationPipe())
  app.useGlobalFilters(new HttpExceptionFilter())
  app.useGlobalInterceptors(new TransformInterceptor())
  await app.listen(3000)
}

bootstrap()
